﻿using ERPPublic.eneity;
using MediatR;
using PersonnelManagement.Application.Command.ManpowerCommand;
using PersonnelManagement.Domain.Dto;
using PersonnelManagement.Domain.PersonnelDomain;
using PersonnelManagement.Dtos;
using PersonnelManagement.Infrstructre;
using PersonnelManagement.Infrstructre.Interface;
using SqlSugar;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace PersonnelManagement.Application.CommandHandler.ManpowerCommandHandler
{
    public class ListManpowerCommandHandler : IRequestHandler<ListManpowerCommand, PageDto<ListManpowerDto>>
    {

        ISectorRepository sRep; //部门
        IPostRepository pRep;   //岗位
        IManpowerRepostiory mRep;   //人员档案
        ISqlSugarClient db;

        public ListManpowerCommandHandler(ISectorRepository sRep, IPostRepository pRep, IManpowerRepostiory mRep, ISqlSugarClient db)
        {
            this.sRep = sRep;
            this.pRep=pRep;
            this.mRep = mRep;
            this.db = db;
        }

        public async Task<PageDto<ListManpowerDto>> Handle(ListManpowerCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<Manpower>()
                .InnerJoin<Sector>((m, s) => m.SectorId == s.SectorId)
                .InnerJoin<Post>((m, s, p) => s.SectorId == p.SectorId)
                .Where(m => m.IsDel == false)
                .Select((m, s, p) => new ListManpowerDto
                {
                    ManpowerId = m.ManpowerId,
                    MNumber = m.MNumber,
                    MName = m.MName,
                    Users = m.Users,
                    MSex = m.MSex,
                    MPhone = m.MPhone,
                    MEmail=m.MEmail,
                    MIdCard = m.MIdCard,
                    SectorId = m.SectorId,
                    PostId = m.PostId,
                    MBirthDay=m.MBirthDay,
                    MWeChat=m.MWeChat,
                    MQQ=m.MQQ,
                    MContact=m.MContact,
                    MCPhone=m.MCPhone,
                    MPermanent=m.MPermanent,
                    MResidence=m.MResidence,
                    MIdPhoto=m.MIdPhoto,
                    MEntryDate=m.MEntryDate,
                    SectorName = s.SectorName,
                    PostName=p.PostName,
                }).ToList().DistinctBy(m => m.ManpowerId);

            //真实姓名
            if (!string.IsNullOrEmpty(request.MName))
            {
                list = list.Where(c => c.MName.Contains(request.MName));
            }
            //入职时间
            if (request.Mstart != null)
            {
                list = list.Where(c => c.MEntryDate>=request.Mstart);
            }
            if (request.Mend != null)
            {
                list = list.Where(c => c.MEntryDate < request.Mend);
            }
            //出生时间
            if (request.Bstart != null)
            {
                list = list.Where(c => c.MBirthDay >= request.Bstart);
            }
            if (request.Bend != null)
            {
                list = list.Where(c => c.MBirthDay < request.Bend);
            }
            //部门
            if (request.SectorId != null)
            {
                list = list.Where(c => c.SectorId == request.SectorId);
            }
            //部门
            if (request.PostId != null)
            {
                list = list.Where(c => c.PostId == request.PostId);
            }


            PageDto<ListManpowerDto> page = new PageDto<ListManpowerDto>();
            page.lcount = list.Count();
            page.pcount = (int)Math.Ceiling(page.lcount * 1.0 / request.Psize);
            page.list = list.OrderByDescending(x => x.ManpowerId).Skip((request.Pindex - 1) * request.Psize).Take(request.Psize).ToList();
            return page;

        }


    }
}
